home *** CD-ROM | disk | FTP | other *** search
- /*
- File: DictionaryDialog.c
-
- Contains: A Sample application for dictionary access.
-
- Version: Technology: System 8
- Release: Daruma Developer Release 1
-
- Copyright: 1998 by Apple Computer, Inc., all rights reserved
-
- Contact: daruma@apple.com
-
- */
-
-
- #include <AEDataModel.h>
- #include <Appearance.h>
- #include <StandardFile.h>
- #include <Lists.h>
- #include <Resources.h>
- #include <NumberFormatting.h> // for NumToString()
- #include "DictionaryAccess.h"
- #include "FunctionProto.h"
-
- // ========================================================================================
- // Prototypes for static functions
- // ========================================================================================
- static DictionaryDialogPtr CreateDictionaryDialog ( const FSSpec *dictionaryFile );
- static void DoDictionaryDialogEvent ( DictionaryDialogPtr dictDialog );
- static void DisposeDictionaryDialog( DictionaryDialogPtr dictDialog);
- static void FindDictionaryRecord( DictionaryDialogPtr dictDialog);
- static void AddDictionaryRecord( DictionaryDialogPtr dictDialog);
- static void RemoveDictionaryRecord( DictionaryDialogPtr dictDialog);
- static void SelectDictionaryHinshi( DictionaryDialogPtr dictDialog);
- static void SelectDictionaryFoundKeyList( DictionaryDialogPtr dictDialog);
- static void EraseFoundFieldText( DictionaryDialogPtr dictDialog);
- static void EraseFoundListContents( DictionaryDialogPtr dictDialog);
- static OSStatus SetUpDictionaryKeyPopup( DictionaryDialogPtr dictDialog);
- static void ChangeDictionaryKey( DictionaryDialogPtr dictDialog);
- static OSStatus SetupFindMethodPopup( DictionaryDialogPtr dictDialog);
- static void ChangeFindMethod( DictionaryDialogPtr dictDialog);
- static void CheckReadOnlyDictionary( DictionaryDialogPtr dictDialog);
- static pascal Boolean MyModalDialogFilter( DialogPtr dialog, EventRecord *theEvent, DialogItemIndex *itemHit);
-
-
- // ========================================================================================
- // DoOpenDictionaryFile
- // ========================================================================================
- void DoOpenDictionaryFile( void )
- {
- DictionaryDialogPtr dictDialog;
- SInt16 numTypes;
- SFTypeList typeList;
- StandardFileReply reply;
-
- numTypes = 1;
- typeList[0] = 'dict';
-
- // Select dictionary file
- StandardGetFile( NULL, numTypes, typeList, &reply);
- if ( !reply.sfGood ) return;
-
- // Create dictionary dialog
- dictDialog = CreateDictionaryDialog( &reply.sfFile);
-
- if ( dictDialog != NULL)
- {
- // Handle event
- DoDictionaryDialogEvent( dictDialog);
-
- // Dispose dictionary dialog
- DisposeDictionaryDialog( dictDialog);
- }
- }
-
-
- // ========================================================================================
- // CreateDictionaryDialog
- // ========================================================================================
- static DictionaryDialogPtr CreateDictionaryDialog ( const FSSpec *dictionaryFile )
- {
- OSStatus err;
- DialogPtr dialog;
- DictionaryDialogPtr dictDialog;
- ControlHandle controlHandle;
-
- //------------------------------------------------------------------------------
- // Create dialog
- //
- dictDialog = (DictionaryDialogPtr)NewPtrClear( sizeof( DictionaryDialogRec));
- require_action( dictDialog, allocDialogInfo_Failure, err = memFullErr;);
-
- dialog = GetNewDialog( kDictionaryDialogResID, dictDialog, (WindowRef)-1L);
- require_action( dialog, getDialog_Failure, err = ResError(););
-
- SetWTitle( dialog, dictionaryFile->name);
-
- dictDialog->dictionaryFile = *dictionaryFile;
- dictDialog->dictionaryID = kDCMInvalidObjectID;
- dictDialog->dictionaryRef = kDCMInvalidObjectRef;
- dictDialog->lastSelectedListIndex = -1;
-
- //------------------------------------------------------------------------------
- // Check that the dictionary file is already registered
- //
- err = DCMGetDictionaryIDFromFile( dictionaryFile, &dictDialog->dictionaryID);
- if ( err != noErr )
- {
- // Register this dictionary temporary
- err = DCMRegisterDictionaryFile( dictionaryFile, &dictDialog->dictionaryID);
- nrequire( err, registerDictionary_Failure);
-
- dictDialog->registeredByMe = true;
- }
-
- //------------------------------------------------------------------------------
- // Open dictionary
- //
- err = DCMOpenDictionary( dictDialog->dictionaryID, 0L, NULL, &dictDialog->dictionaryRef);
- nrequire( err, openDictionary_Failure);
-
- //------------------------------------------------------------------------------
- // Setup key popup menu
- //
- err = SetUpDictionaryKeyPopup( dictDialog);
- nrequire( err, setupKeyMenu_Failure);
-
- //------------------------------------------------------------------------------
- // Setup find method popup menu
- //
- err = SetupFindMethodPopup( dictDialog);
- nrequire( err, setupFindMethod_Failure);
-
- //------------------------------------------------------------------------------
- // Check that the dictionary is read only or writable
- //
- CheckReadOnlyDictionary( dictDialog);
-
- //------------------------------------------------------------------------------
- // Set default/cancel button and select edit text item
- //
- SetDialogDefaultItem( dialog, kFindBtnDItemID);
- SetDialogCancelItem( dialog, kDoneBtnDItemID);
-
- GetDialogItemAsControl( dialog, kKeyEditTextDItemID, &controlHandle);
- SetKeyboardFocus( (WindowPtr)dialog, controlHandle, kControlEditTextPart);
-
- ShowWindow( (WindowPtr)dialog);
-
- return dictDialog;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- setupFindMethod_Failure:
- setupKeyMenu_Failure:
- DCMCloseDictionary( dictDialog->dictionaryRef);
- openDictionary_Failure:
- if ( dictDialog->registeredByMe)
- DCMUnregisterDictionary( dictDialog->dictionaryID);
- registerDictionary_Failure:
- CloseDialog( dialog);
- getDialog_Failure:
- DisposePtr( (Ptr)dictDialog);
- allocDialogInfo_Failure:
- return NULL;
- }
-
-
- // ========================================================================================
- // DoDictionaryDialogEvent
- // ========================================================================================
- static void DoDictionaryDialogEvent ( DictionaryDialogPtr dictDialog )
- {
- SInt16 itemHit = 0;
- ModalFilterUPP filterUPP;
-
- filterUPP = NewModalFilterProc( MyModalDialogFilter);
-
- do
- {
- ModalDialog( filterUPP, &itemHit);
-
- switch ( itemHit)
- {
- case kFindBtnDItemID:
- FindDictionaryRecord( dictDialog);
- break;
-
- case kAddBtnDItemID:
- AddDictionaryRecord( dictDialog);
- break;
-
- case kRemoveBtnDItemID:
- RemoveDictionaryRecord( dictDialog);
- break;
-
- case kKeyPopupDItemID:
- ChangeDictionaryKey( dictDialog);
- break;
-
- case kFindMethodPopupDItemID:
- ChangeFindMethod( dictDialog);
- break;
-
- case kHinshiPopupDItemID:
- SelectDictionaryHinshi( dictDialog);
- break;
-
- case kFoundKeyListDItemID:
- SelectDictionaryFoundKeyList( dictDialog);
- break;
-
- default: break;
- }
- }
- while ( itemHit != kDoneBtnDItemID);
-
- DisposeRoutineDescriptor( filterUPP);
- }
-
-
- // ========================================================================================
- // DisposeDictionaryDialog
- // ========================================================================================
- static void DisposeDictionaryDialog( DictionaryDialogPtr dictDialog)
- {
- //------------------------------------------------------------------------------
- // Close and unregister dictionary (if needed)
- //
- DCMCloseDictionary( dictDialog->dictionaryRef);
-
- if ( dictDialog->registeredByMe)
- DCMUnregisterDictionary( dictDialog->dictionaryID);
-
- //------------------------------------------------------------------------------
- // Free memory
- //
- if ( dictDialog->foundUniqueIDs != NULL )
- DisposePtr( (Ptr)dictDialog->foundUniqueIDs);
-
- CloseDialog( (DialogPtr)dictDialog);
- DisposePtr( (Ptr)dictDialog);
- }
-
-
- // ========================================================================================
- // FindDictionaryRecord
- // ========================================================================================
- static void FindDictionaryRecord( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- ListHandle listHandle;
- ControlHandle controlHandle;
- Str255 string;
- DCMFoundRecordIterator recordIterator;
- ByteCount actualSize;
- ItemCount foundRecordCount, i;
- Cell listCell = { 0, 0};
-
- //------------------------------------------------------------------------------
- // Clean up display
- //
- EraseFoundListContents( dictDialog);
- EraseFoundFieldText( dictDialog);
-
- //------------------------------------------------------------------------------
- // Get list handle
- //
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kFoundKeyListDItemID, &controlHandle);
- nrequire( err, getControl_Failure);
-
- listHandle = GetListHandleFromControl( controlHandle);
- require( listHandle, getListHandle_Failure);
-
- //------------------------------------------------------------------------------
- // Get key string
- //
- GetDialogEditTextItemString( (DialogPtr)dictDialog, kKeyEditTextDItemID, string);
-
- //------------------------------------------------------------------------------
- // Search dictionary
- // In this sample, finds records without any pre-fetched data. So, all data should
- // be fetched later by using DCMGetFieldData(). That's a suitable way for this sample.
- // In general, however, method using pre-fetched data is faster than that using DCMGetFieldData().
- //
- err = DCMFindRecords( dictDialog->dictionaryRef, dictDialog->currentKeyField, string[0],
- &string[1], dictDialog->currentFindMethod, 0, NULL, 0, 0, &recordIterator);
- if ( err != dcmNoRecordErr)
- nrequire( err, findRecord_Failure);
-
- //------------------------------------------------------------------------------
- // Create uniqueID array for saving uniqueID of each found record
- //
- foundRecordCount = DCMCountRecordIterator( recordIterator);
-
- if ( dictDialog->foundUniqueIDs != NULL)
- DisposePtr( (Ptr)dictDialog->foundUniqueIDs);
-
- dictDialog->foundUniqueIDs = (DCMUniqueID *)NewPtrClear( sizeof( DCMUniqueID) * foundRecordCount);
- require_action( dictDialog->foundUniqueIDs, createUniqueIDList_Failure, err = memFullErr;);
-
- //------------------------------------------------------------------------------
- // Add found record's keys into list
- //
- LSetDrawingMode( false, listHandle);
- for ( i = 0; i < foundRecordCount; i++ )
- {
- err = DCMIterateFoundRecord( recordIterator, sizeof( string) - 1, &actualSize,
- &string[1], &dictDialog->foundUniqueIDs[i], NULL);
- nrequire( err, iterateRecord_Failure);
- string[0] = actualSize;
-
- listCell.v = LAddRow( 1, i, listHandle);
- LSetCell( &string[1], string[0], listCell, listHandle);
- }
- LSetDrawingMode( true, listHandle);
- DrawOneControl( controlHandle);
-
- //------------------------------------------------------------------------------
- // Dispose iterator object (don't forget it!)
- //
- DCMDisposeRecordIterator( recordIterator);
-
- return;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- iterateRecord_Failure:
- createUniqueIDList_Failure:
- DCMDisposeRecordIterator( recordIterator);
- findRecord_Failure:
- getListHandle_Failure:
- getControl_Failure:
- return;
- }
-
-
- // ========================================================================================
- // AddDictionaryRecord
- // ========================================================================================
- static void AddDictionaryRecord( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- AERecord recordData = { typeNull, NULL};
- DCMFieldTag dataField;
- Str255 keyString, dataString, tempString;
- long tempNum;
- HomographWeight wordWeight;
- JapanesePartOfSpeech hinshiCode;
- DCMUniqueID newUniqueID;
-
- //------------------------------------------------------------------------------
- // Prepare data
- //
- GetDialogEditTextItemString( (DialogPtr)dictDialog, kKeyEditTextDItemID, keyString);
- GetDialogEditTextItemString( (DialogPtr)dictDialog, kDataEditTextDItemID, dataString);
-
- GetDialogEditTextItemString( (DialogPtr)dictDialog, kWeightEditTextDItemID, tempString);
- StringToNum( tempString, &tempNum);
- wordWeight = tempNum;
-
- GetDialogStaticTextItemString( (DialogPtr)dictDialog, kHinshiStatTextDItemID, tempString);
- {
- //
- // Convert hinshi name to hinshi code using coercion.
- //
- AEDesc hinshiDesc = { typeNull, NULL};
-
- err = AECoercePtr( typeChar, &tempString[1], tempString[0], kDCMJapaneseHinshiType, &hinshiDesc);
- if ( err == noErr)
- BlockMoveData( *hinshiDesc.dataHandle, &hinshiCode, sizeof(hinshiCode));
- else
- hinshiCode = 0;
-
- AEDisposeDesc( &hinshiDesc);
- }
-
- //------------------------------------------------------------------------------
- // Create a data record for adding new entry to the dictionary
- //
- err = AECreateList( NULL, 0L, true, &recordData);
- nrequire( err, createRecord_Failure);
-
- // Set data string
- dataField = ( dictDialog->currentKeyField == kDCMJapaneseYomiTag ? kDCMJapaneseHyokiTag : kDCMJapaneseYomiTag);
- err = AEPutKeyPtr( &recordData, dataField, typeChar, &dataString[1], dataString[0]);
- nrequire( err, addDataString_Failure);
-
- // Set hinshi
- err = AEPutKeyPtr( &recordData, kDCMJapaneseHinshiTag, kDCMJapaneseHinshiType, &hinshiCode, sizeof(hinshiCode));
- nrequire( err, addHinshi_Failure);
-
- // Set word weight
- err = AEPutKeyPtr( &recordData, kDCMJapaneseWeightTag, kDCMJapaneseWeightType, &wordWeight, sizeof(wordWeight));
- nrequire( err, addWordWeight_Failure);
-
- //------------------------------------------------------------------------------
- // At first, get write access of the dictionary
- //
- err = DCMGetDictionaryWriteAccess( dictDialog->dictionaryRef, 0L);
- nrequire( err, getWriteAccess_Failure);
-
- //------------------------------------------------------------------------------
- // Add new record to the dictionary
- //
- err = DCMAddRecord( dictDialog->dictionaryRef, dictDialog->currentKeyField, keyString[0],
- &keyString[1], false, &recordData, &newUniqueID);
- // dcmDupRecordErr is NOT an error, ignore it
- if ( err != dcmDupRecordErr)
- nrequire( err, addRecord_Failure);
-
- //------------------------------------------------------------------------------
- // Release write access as soon as possible
- //
- DCMReleaseDictionaryWriteAccess( dictDialog->dictionaryRef, true);
-
- //------------------------------------------------------------------------------
- // Clean up
- //
- AEDisposeDesc( &recordData);
-
- return;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- addRecord_Failure:
- DCMReleaseDictionaryWriteAccess( dictDialog->dictionaryRef, true);
- getWriteAccess_Failure:
- addWordWeight_Failure:
- addHinshi_Failure:
- addDataString_Failure:
- AEDisposeDesc( &recordData);
- createRecord_Failure:
- return;
- }
-
-
- // ========================================================================================
- // RemoveDictionaryRecord
- // ========================================================================================
- static void RemoveDictionaryRecord( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- Str255 keyString;
-
- //------------------------------------------------------------------------------
- // Get target key string
- //
- GetDialogEditTextItemString( (DialogPtr)dictDialog, kKeyEditTextDItemID, keyString);
-
- //------------------------------------------------------------------------------
- // Get write access of the target dictionary
- //
- err = DCMGetDictionaryWriteAccess( dictDialog->dictionaryRef, 0L);
- nrequire( err, getWriteAccess_Failure);
-
- //------------------------------------------------------------------------------
- // Delete a record from dictionary
- //
- err = DCMDeleteRecord( dictDialog->dictionaryRef, dictDialog->currentKeyField,
- keyString[0], &keyString[1], dictDialog->lastSelectedUniqueID);
- nrequire( err, deleteRecord_Failure);
-
- //------------------------------------------------------------------------------
- // Release write access of target dictionary
- //
- DCMReleaseDictionaryWriteAccess( dictDialog->dictionaryRef, true);
-
- //------------------------------------------------------------------------------
- // Update display
- //
- SetDialogEditTextItemString( (DialogPtr)dictDialog, kKeyEditTextDItemID, "\p");
- EraseFoundFieldText( dictDialog);
- EraseFoundListContents( dictDialog);
-
- return;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- deleteRecord_Failure:
- DCMReleaseDictionaryWriteAccess( dictDialog->dictionaryRef, false);
- getWriteAccess_Failure:
- return;
- }
-
-
- // ========================================================================================
- // SelectDictionaryHinshi
- // ========================================================================================
- static void SelectDictionaryHinshi( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- ControlHandle popupControl;
- SInt16 selectedItem;
- Str255 hinshiName;
- MenuHandle hinshiMenu = GetMenuHandle( kHinshiPopupMenuID);
-
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kHinshiPopupDItemID, &popupControl);
- nrequire( err, getPopupControl_Failure);
-
- selectedItem = GetControlValue( popupControl);
- GetMenuItemText( hinshiMenu, selectedItem, hinshiName);
-
- SetDialogStaticTextItemString( (DialogPtr)dictDialog, kHinshiStatTextDItemID, hinshiName);
-
- return;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- getPopupControl_Failure:
- return;
- }
-
-
- // ========================================================================================
- // SelectDictionaryFoundKeyList
- // ========================================================================================
- static void SelectDictionaryFoundKeyList( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- ListHandle listHandle;
- ControlHandle controlHandle;
- Cell listCell = { 0, 0};
- Str255 keyString, dataString;
- Str31 hinshiString;
- Str15 weightString;
- AEDesc dataList = { typeNull, NULL};
- DescType actualType;
- Size actualSize;
- HomographWeight wordWeight;
- JapanesePartOfSpeech hinshiCode;
-
- //------------------------------------------------------------------------------
- // Get list handle
- //
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kFoundKeyListDItemID, &controlHandle);
- nrequire( err, getControl_Failure);
-
- listHandle = GetListHandleFromControl( controlHandle);
- require( listHandle, getListHandle_Failure);
-
- //------------------------------------------------------------------------------
- // Get the selected cell
- //
- if ( !LGetSelect( true, &listCell, listHandle) )
- listCell.v = -1;
-
- if ( dictDialog->lastSelectedListIndex == listCell.v) // selected cell is not changed
- return;
- else
- dictDialog->lastSelectedListIndex = listCell.v;
-
- //------------------------------------------------------------------------------
- // Get dictionary data of the selected record
- //
- if ( dictDialog->lastSelectedListIndex != -1 ) // there is selected item
- {
- ItemCount neededDataNum = 3;
- DCMFieldTag neededDataTags[3] = { '????', kDCMJapaneseWeightTag, kDCMJapaneseHinshiTag};
- SInt16 cellDataLen;
-
- cellDataLen = sizeof( keyString) - 1; // This line is easy to forget !
- LGetCell( &keyString[1], &cellDataLen, listCell, listHandle);
- keyString[0] = cellDataLen;
-
- // Get yomi or hyouki, which is not set to the find key
- neededDataTags[0] = ( dictDialog->currentKeyField == kDCMJapaneseYomiTag ? kDCMJapaneseHyokiTag : kDCMJapaneseYomiTag);
-
- // Get dictionary field data of the specified record
- err = DCMGetFieldData( dictDialog->dictionaryRef, dictDialog->currentKeyField,
- cellDataLen, &keyString[1], dictDialog->foundUniqueIDs[listCell.v],
- neededDataNum, neededDataTags, &dataList);
- nrequire( err, getFieldData_Failure);
-
- // Retrieve yomi or hyouki string
- err = AEGetKeyPtr( &dataList, neededDataTags[0], typeChar,
- &actualType, &dataString[1], sizeof(dataString) - 1, &actualSize);
- nrequire( err, getDataString_Failure);
- dataString[0] = actualSize;
-
- // Retrieve word weight and convert it to string
- err = AEGetKeyPtr( &dataList, neededDataTags[1], kDCMJapaneseWeightType,
- &actualType, &wordWeight, sizeof(wordWeight), &actualSize);
- nrequire( err, getWordWeight_Failure);
- NumToString( wordWeight, weightString);
-
- // Retrieve hinshi code
- err = AEGetKeyPtr( &dataList, neededDataTags[2], kDCMJapaneseHinshiType,
- &actualType, &hinshiCode, sizeof(hinshiCode), &actualSize);
- nrequire( err, getHinshiCode_Failure);
-
- // You can convert kDCMJapaneseHinshiType to typeChar by coercing data type.
- // It means that you can get the hinshi name from hinshi code.
- // Note that you cannot get hinshi name directly from AEDesc.
- {
- AEDesc hinshiDesc = { typeNull, NULL};
-
- err = AECoercePtr( kDCMJapaneseHinshiType, &hinshiCode, sizeof(hinshiCode),
- typeChar, &hinshiDesc);
- nrequire( err, convertHinshiCode_Failure);
-
- actualSize = GetHandleSize( hinshiDesc.dataHandle);
- BlockMoveData( *hinshiDesc.dataHandle, &hinshiString[1], actualSize);
- hinshiString[0] = actualSize;
- AEDisposeDesc( &hinshiDesc);
- }
-
- // Save uniqueID of the selected record
- dictDialog->lastSelectedUniqueID = dictDialog->foundUniqueIDs[listCell.v];
-
- AEDisposeDesc( &dataList); // Don't forget it
- }
- else // no list item is selected
- {
- keyString[0] = 0;
- dataString[0] = 0;
- hinshiString[0] = 0;
- weightString[0] = 0;
- }
-
- //------------------------------------------------------------------------------
- // Set retrieved strings to dialog field
- //
- SetDialogEditTextItemString( (DialogPtr)dictDialog, kKeyEditTextDItemID, keyString);
- SetDialogEditTextItemString( (DialogPtr)dictDialog, kDataEditTextDItemID, dataString);
- SetDialogEditTextItemString( (DialogPtr)dictDialog, kWeightEditTextDItemID, weightString);
- SetDialogStaticTextItemString( (DialogPtr)dictDialog, kHinshiStatTextDItemID, hinshiString);
-
- return;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- convertHinshiCode_Failure:
- getHinshiCode_Failure:
- getWordWeight_Failure:
- getDataString_Failure:
- AEDisposeDesc( &dataList);
- getFieldData_Failure:
- getListHandle_Failure:
- getControl_Failure:
- return;
- }
-
-
- // ========================================================================================
- // EraseFoundFieldText
- // ========================================================================================
- static void EraseFoundFieldText( DictionaryDialogPtr dictDialog)
- {
- SetDialogEditTextItemString( (DialogPtr)dictDialog, kDataEditTextDItemID, "\p");
- SetDialogEditTextItemString( (DialogPtr)dictDialog, kWeightEditTextDItemID, "\p");
- SetDialogStaticTextItemString( (DialogPtr)dictDialog, kHinshiStatTextDItemID, "\p");
- }
-
-
- // ========================================================================================
- // EraseFoundListContents
- // ========================================================================================
- static void EraseFoundListContents( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- ListHandle listHandle;
- ControlHandle controlHandle;
-
- //------------------------------------------------------------------------------
- // Get list handle
- //
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kFoundKeyListDItemID, &controlHandle);
-
- if ( err == noErr )
- {
- listHandle = GetListHandleFromControl( controlHandle);
-
- if ( listHandle != NULL )
- {
- // Delete list contents
- LDelRow( 0, 0, listHandle);
- dictDialog->lastSelectedListIndex = -1;
- LSetDrawingMode( true, listHandle);
- DrawOneControl( controlHandle);
- }
- }
- }
-
-
- // ========================================================================================
- // SetUpDictionaryKeyPopup
- // ========================================================================================
- static OSStatus SetUpDictionaryKeyPopup( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- AERecord fieldInfo = { typeNull, NULL};
- UInt16 i, keyCount;
- DCMFieldTag fieldTags[2] = { kDCMJapaneseYomiTag, kDCMJapaneseHyokiTag};
- DCMFieldAttributes attribute;
- Str255 fieldName;
- ControlHandle popupControl;
- MenuHandle keyMenuHandle = GetMenuHandle( kKeyPopupMenuID);
-
- //------------------------------------------------------------------------------
- // Remove old menu items
- //
- while ( CountMItems( keyMenuHandle) > 0)
- DeleteMenuItem( keyMenuHandle, 1);
-
- //------------------------------------------------------------------------------
- // Get yomi and hyouki field information
- //
- for ( keyCount = 0, i = 0; i < 2; i++ )
- {
- err = DCMGetDictionaryFieldInfo( dictDialog->dictionaryID, fieldTags[i], &fieldInfo);
- nrequire( err, getFieldInfo_Failure);
-
- err = DCMGetFieldAttributes( &fieldInfo, &attribute);
- nrequire( err, getFieldAttr_Failure);
-
- if ( attribute & kDCMIndexedFieldMask) // indexed field
- {
- err = DCMGetFieldName( &fieldInfo, fieldName);
- nrequire( err, getFieldName_Failure);
-
- AppendMenu( keyMenuHandle, "\pDummy");
- SetMenuItemText( keyMenuHandle, keyCount + 1, fieldName);
-
- dictDialog->keyFields[keyCount] = fieldTags[i];
- keyCount++;
- }
- AEDisposeDesc( &fieldInfo);
- }
-
- //------------------------------------------------------------------------------
- // Setup initial setting
- //
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kKeyPopupDItemID, &popupControl);
- nrequire( err, getPopupControl_Failure);
-
- SetControlMaximum( popupControl, keyCount);
- SetControlValue( popupControl, 1);
- ChangeDictionaryKey( dictDialog);
-
- return noErr;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- getPopupControl_Failure:
- getFieldName_Failure:
- getFieldAttr_Failure:
- getFieldInfo_Failure:
- AEDisposeDesc( &fieldInfo);
- return err;
- }
-
-
- // ========================================================================================
- // ChangeDictionaryKey
- // ========================================================================================
- static void ChangeDictionaryKey( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- ControlHandle popupControl;
- SInt16 index;
-
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kKeyPopupDItemID, &popupControl);
- nrequire( err, getPopupControl_failure);
-
- index = GetControlValue( popupControl);
-
- if ( dictDialog->currentKeyField == dictDialog->keyFields[index - 1]) // not changed
- return;
-
- dictDialog->currentKeyField = dictDialog->keyFields[index - 1];
-
- //------------------------------------------------------------------------------
- // Change dialog's text field label
- //
- if ( dictDialog->currentKeyField == kDCMJapaneseYomiTag )
- {
- HideDialogItem( (DialogPtr)dictDialog, kYomiTitleStatTextDItemID);
- ShowDialogItem( (DialogPtr)dictDialog, kHyoukiTitleStatTextDItemID);
- }
- else
- {
- HideDialogItem( (DialogPtr)dictDialog, kHyoukiTitleStatTextDItemID);
- ShowDialogItem( (DialogPtr)dictDialog, kYomiTitleStatTextDItemID);
- }
-
- return;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- getPopupControl_failure:
- return;
- }
-
-
- // ========================================================================================
- // SetupFindMethodPopup
- // ========================================================================================
- static OSStatus SetupFindMethodPopup( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- AERecord fieldInfo = { typeNull, NULL};
- UInt32 i, findMethodCount;
- Str31 resourceName;
- StringHandle findMethodNameHandle;
- ControlHandle popupControl;
- MenuHandle findMethodMenuHandle = GetMenuHandle( kFindMethodPopupMenuID);
-
- //------------------------------------------------------------------------------
- // Remove old menu items
- //
- while ( CountMItems( findMethodMenuHandle) > 0)
- DeleteMenuItem( findMethodMenuHandle, 1);
-
- //------------------------------------------------------------------------------
- // Get key field information
- //
- err = DCMGetDictionaryFieldInfo( dictDialog->dictionaryID, dictDialog->currentKeyField, &fieldInfo);
- nrequire( err, getFieldInfo_Failure);
-
- err = DCMGetFieldFindMethods( &fieldInfo, dictDialog->findMethods, 5, &findMethodCount);
- nrequire( err, getFindMethod_Failure);
-
- //------------------------------------------------------------------------------
- // Add find method name to the popup menu
- //
- for ( i = 0; i < findMethodCount; i++ )
- {
- // Convert DCMFindMethod to pascal string
- BlockMoveData( &dictDialog->findMethods[i], &resourceName[1], sizeof( DCMFindMethod));
- resourceName[0] = sizeof( DCMFindMethod);
-
- // Get find method name from resource
- findMethodNameHandle = (StringHandle)Get1NamedResource( 'STR ', resourceName);
- if ( findMethodNameHandle == NULL)
- findMethodNameHandle = (StringHandle)Get1Resource( 'STR ', 200);
- HLock( (Handle)findMethodNameHandle);
-
- AppendMenu( findMethodMenuHandle, "\pDummy");
- SetMenuItemText( findMethodMenuHandle, i + 1, *findMethodNameHandle);
- HUnlock( (Handle)findMethodNameHandle);
- ReleaseResource( (Handle)findMethodNameHandle);
- }
-
- //------------------------------------------------------------------------------
- // Setup initial setting
- //
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kFindMethodPopupDItemID, &popupControl);
- nrequire( err, getPopupControl_Failure);
-
- SetControlMaximum( popupControl, findMethodCount);
- SetControlValue( popupControl, 1);
- ChangeFindMethod( dictDialog);
-
- return noErr;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- getPopupControl_Failure:
- getFindMethod_Failure:
- getFieldInfo_Failure:
- return err;
- }
-
-
- // ========================================================================================
- // ChangeFindMethod
- // ========================================================================================
- static void ChangeFindMethod( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- ControlHandle popupControl;
- SInt16 index;
-
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kFindMethodPopupDItemID, &popupControl);
- nrequire( err, getPopupControl_Failure);
-
- index = GetControlValue( popupControl);
-
- dictDialog->currentFindMethod = dictDialog->findMethods[index - 1];
-
- return;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- getPopupControl_Failure:
- return;
- }
-
-
- // ========================================================================================
- // CheckReadOnlyDictionary
- // ========================================================================================
- static void CheckReadOnlyDictionary( DictionaryDialogPtr dictDialog)
- {
- OSStatus err;
- SInt16 permission;
- ByteCount actualSize;
- ControlHandle controlHandle;
-
- //------------------------------------------------------------------------------
- // Get permission property of the dictionary
- //
- err = DCMGetDictionaryProperty( dictDialog->dictionaryID, pDCMPermission,
- sizeof( permission), &actualSize, &permission);
- nrequire( err, getPermProperty_Failure);
-
- //------------------------------------------------------------------------------
- // Disable add/remove button if the dictionary is read only
- //
- if ( permission != kDCMReadWriteDictionary)
- {
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kAddBtnDItemID, &controlHandle);
- if ( err == noErr)
- DeactivateControl( controlHandle);
-
- err = GetDialogItemAsControl( (DialogPtr)dictDialog, kRemoveBtnDItemID, &controlHandle);
- if ( err == noErr)
- DeactivateControl( controlHandle);
- }
-
- return;
-
- //`````````````````````````````````````````````````````````````````````````````
- // Error handling
- //
- getPermProperty_Failure:
- return;
- }
-
-
- // ========================================================================================
- // MyModalDialogFilter
- // ========================================================================================
- static pascal Boolean MyModalDialogFilter( DialogPtr dialog, EventRecord *theEvent, DialogItemIndex *itemHit)
- {
- Boolean eventProcessed = false;
-
- //------------------------------------------------------------------------------
- // Handle return/enter key
- //
- if ( theEvent->what == keyDown || theEvent->what == autoKey )
- {
- UInt8 charCode = theEvent->message & charCodeMask;
-
- if ( charCode == kEnterCharCode || charCode == kReturnCharCode)
- {
- *itemHit = kFindBtnDItemID;
- HiliteButtonDialogItem( dialog, kFindBtnDItemID);
- eventProcessed = true;
- }
- }
-
- //------------------------------------------------------------------------------
- // Transfer un-handled event to the default filter
- //
- if ( !eventProcessed )
- eventProcessed = StdFilterProc( dialog, theEvent, itemHit);
-
- return eventProcessed;
- }
-
-
-
-
-
-
-
-
-